package server.content.skills.mining;

import core.util.Misc;
import server.content.skilling.PlayerSkill;
import server.content.skills.misc.SkillConstants;
import server.event.CycleEvent;
import server.event.CycleEventContainer;
import server.event.CycleEventHandler;
import server.event.Event;
import server.event.EventContainer;
import server.event.EventManager;
import server.game.players.Client;

public class MMining extends PlayerSkill {

	private final int PICKAXES[] = { 1265, 1267, 1269, 1271 };
	private final int ANIMS[][] = { { 1275, 624 }, { 1271, 628 },
			{ 1273, 629 }, { 1269, 627 }, { 1267, 626 }, { 1265, 625 }, };
	private final int PICKAXE_LVL[][] = { { 1265, 1 }, { 1267, 1 },
			{ 1269, 5 }, { 1271, 31 }, { 1273, 21 }, { 1275, 41 } };
	private Rock myRock = null;

	public MMining(final Client c, final int skillId) {
		super(c, skillId);
		setTick(2);
	}

	public void init() {
		kill();
		getClient().turnPlayerTo(getClient().objectX, getClient().objectY);
		if (getClient().getItems().freeSlots() < 1) {
			getClient().sendMessage(
					"Your inventory is too full to mine anymore ores.");
			kill();
			return;
		}
		if (getMyPickAxe() < 0) {
			kill();
			return;
		}
		if (getPlayerLevel() < getAxeLvl()) {
			kill();
			getClient().sendMessage(
					"You need a mining level of " + getAxeLvl()
							+ " to use this pickaxe.");
			return;
		}
		if (System.currentTimeMillis() - getClient().lastThieve < 2500) {
			kill();
			return;
		}
		if (!MineHandler.rockExists(getClient().objectX, getClient().objectY)) {
			MineHandler.addRock(getClient(), getClient().objectId,
					getClient().objectX, getClient().objectY);
			kill();
			return;
		} else {
			myRock = MineHandler.getRock(getClient().objectX,
					getClient().objectY);
			if (myRock.getLvlReq() <= getClient().playerLevel[SkillConstants.MINING]) {
				if (antiBotEnabled()) {
					getClient().getDH().sendStartInfo("",
							"CoolDown: " + coolDown, "", "", "Timer");
					// kill();
					return;
				}
				if (startTime < 0)
					startTime = System.currentTimeMillis();
				getClient().sendMessage("You swing your pickaxe at the rock.");
				setActive(true);
				doAnimation(getMyAnim());
				getClient().lastThieve = System.currentTimeMillis();
				process();
			} else {
				getClient().sendMessage(
						"You need a mining level of " + myRock.getLvlReq()
								+ " to mine this rock.");
				kill();
				return;
			}
		}
	}

	public void process() {
		CycleEventHandler.getSingleton().addEvent(getClient(),
				new CycleEvent() {

					@Override
					public void execute(CycleEventContainer container) {
						if (isActive()) {
							if (getClient().getItems().freeSlots() < 1) {
								getClient()
										.sendMessage(
												"Your inventory is too full to mine anymore ores.");
								kill();
								return;
							}
							if (myRock.isDead()) {
								kill();
								return;
							}
							if (coolDown > 0) {
								coolDown();
							}
							doAnimation(getMyAnim());
							if (Misc.random(100) <= getChanceForLevel()) {
								myRock.removeOre();
								getClient().sendMessage(
										"You manage to mine some "
												+ getClient().getItems()
														.getItemName(
																myRock.ORE_ID)
												+ ".");
								addItem(myRock.ORE_ID, 1);
								addXP(myRock.XP, 14);
							}
							final int gems[] = { 1623, 1621, 1619, 1617 };
							if (getPlayerLevel() >= 40) {
								final int randUncut = Misc
										.random(gems.length - 1);
								final int chance = Misc.random(600);
								if (chance > 21 && chance < 33) {
									addItem(gems[randUncut], 1);
									getClient()
											.sendMessage(
													"You manage to mine a "
															+ getClient()
																	.getItems()
																	.getItemName(
																			gems[randUncut])
															+ ".");
								}
							}
						} else {
							container.stop();
						}
					}

					@Override
					public void stop() {

					}
				}, getTick());
	}

	public void kill() {
		myRock = null;
		resetAnim();
		setActive(false);
	}

	public int getChanceForLevel() {
		final int lvl = getPlayerLevel();
		if (lvl > 1 && lvl < 10) {
			return 7;
		} else if (lvl > 9 && lvl < 20) {
			return 15;
		} else if (lvl > 19 && lvl < 39) {
			return 28;
		} else if (lvl > 39 && lvl < 51) {
			return 35;
		} else if (lvl > 50 && lvl < 61) {
			return 42;
		} else if (lvl > 60 && lvl < 71) {
			return 46;
		} else if (lvl > 70 && lvl < 81) {
			return 52;
		} else if (lvl > 80 && lvl < 99) {
			return 56;
		}
		return 10;
	}

	public int getAxeLvl() {
		for (int i = 0; i < PICKAXE_LVL.length; i++) {
			if (PICKAXE_LVL[i][0] == getMyPickAxe()) {
				return PICKAXE_LVL[i][1];
			}
		}
		return 1;
	}

	public int getMyPickAxe() {
		for (int i = 0; i < PICKAXES.length; i++) {
			if (getClient().playerEquipment[getClient().playerWeapon] == PICKAXES[i])
				return PICKAXES[i];
			if (getClient().getItems().playerHasItem(PICKAXES[i]))
				return PICKAXES[i];
		}
		return -1;
	}

	public int getMyAnim() {
		for (int i = 0; i < ANIMS.length; i++) {
			if (ANIMS[i][0] == getMyPickAxe()) {
				return ANIMS[i][1];
			}
		}
		return 65535;
	}

}
